package com.karson.service.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.karson.common.config.redis.RedisConfig;
import com.karson.common.constants.BusinessConstants;
import com.karson.common.model.ActionCounts;
import com.karson.common.model.UserAction;
import com.karson.common.model.UserFavoriteRecord;
import com.karson.common.util.RedisUtil;
import com.karson.service.service.ActionCountsService;
import com.karson.service.service.AlbumInfoService;
import com.karson.service.service.UserActionService;
import com.karson.service.service.UserFavoriteRecordService;
import com.karson.service.dao.UserFavoriteRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author ZKS
* @description 针对表【ar_user_favorite_record(用户收藏记录表)】的数据库操作Service实现
* @createDate 2025-09-04 14:08:51
*/
@Service
public class UserFavoriteRecordServiceImpl extends ServiceImpl<UserFavoriteRecordMapper, UserFavoriteRecord>
    implements UserFavoriteRecordService{
    @Autowired
    private UserFavoriteRecordMapper userFavoriteRecordMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ActionCountsService actionCountsService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByIdBatch(List<Integer> unFavoriteIds, Integer folderId) {
        int uid = StpUtil.getLoginIdAsInt();
        LambdaQueryWrapper<UserFavoriteRecord> queryFavWrapper = new LambdaQueryWrapper<>();
        queryFavWrapper.in(UserFavoriteRecord::getId, unFavoriteIds);
        List<UserFavoriteRecord> userFavoriteRecords = userFavoriteRecordMapper.selectList(queryFavWrapper);
        List<Integer> albumIds = userFavoriteRecords.stream().map(UserFavoriteRecord::getMessageId).toList();
        userFavoriteRecordMapper.removeById(unFavoriteIds);
        for (Integer albumId : albumIds) {
            // 删除后这个专辑是否存在别的收藏夹中
            LambdaQueryWrapper<UserFavoriteRecord> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserFavoriteRecord::getMessageId,albumId);
            queryWrapper.eq(UserFavoriteRecord::getUid,uid);
            queryWrapper.eq(UserFavoriteRecord::getStatus,true);
            Long count = userFavoriteRecordMapper.selectCount(queryWrapper);
            //
            if (count > 0) {
                // 有数据就说明该作品还存在其他收藏夹中，不用更新缓存以及数据库
                continue;
            }
            // 更新数据库
            ActionCounts actionCounts = actionCountsService.getOne(new LambdaQueryWrapper<ActionCounts>().eq(ActionCounts::getMessageId, albumId));
            actionCounts.setFavoriteCount(actionCounts.getFavoriteCount() > 0 ? actionCounts.getFavoriteCount() - 1 : 0);
            actionCountsService.updateById(actionCounts);
            //删除缓存
            redisUtil.removeMember(RedisConfig.ALBUM_USER_FAV + uid,albumId);
            String countKey = RedisConfig.COUNT_KEY + BusinessConstants.BUSINESS_ALBUM + ":" + albumId;
            redisUtil.delete(countKey);
        }
        String key = RedisConfig.ZSET_FAV_KEY + uid + ":" + folderId;
        redisUtil.deleteZSet(key);
    }
}




